#pragma once

#include <iostream>
#include <ctime>
#include <random>
#include <unistd.h>
#include <sys/types.h>

#define TaskNum 3

typedef void (*task_t) (); //task_t 函数指针类型

task_t tasks[TaskNum];

void Print()
{
    std::cout<<"I am prit task"<<std::endl;
}

void Download()
{
    std::cout<<"I am download task"<<std::endl;
}

void Flush()
{
    std::cout<<"I am flush task"<<std::endl;
}


void LoadTask()
{
    srand(time(nullptr));
    tasks[0] = Print;
    tasks[1] = Download;
    tasks[2] = Flush;
}

void ExcuteTask(int num)
{
    if(num < 0 || num > 2)
        return;

    tasks[num]();
}

int SelectTask()
{
    return rand()%TaskNum;
}

void work()
{
    while(true)
    {
        int command = 0;
        int n = read(0,&command,sizeof command);
        if(n == sizeof(int))
        {
            std::cout<<"pid is:"<<getpid()<<"hander task"<<std::endl;
            ExcuteTask(command);
        }
        else if(n == 0)
        {
            std::cout<<"sub process: "<<getpid()<<"quit" << std::endl;
            break;
        }
    }

}